package net.lab1024.sa.admin.module.business.erp.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.sa.admin.module.business.erp.dao.*;
import net.lab1024.sa.admin.module.business.erp.domain.entity.*;
import net.lab1024.sa.admin.module.business.erp.domain.form.add.ApprovordAddForm;
import net.lab1024.sa.admin.module.business.erp.domain.form.query.ApprovordQueryForm;
import net.lab1024.sa.admin.module.business.erp.domain.form.update.ApprovordUpdateForm;
import net.lab1024.sa.admin.module.business.erp.domain.vo.ApprovordVO;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.RequestUser;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.util.SmartRequestUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 审批主表 Service
 *
 * @Author AKITA
 * @Date 2025-04-23 14:10:37
 * @Copyright AKITA
 */

@Service
public class ApprovordService {

    @Resource
    private ApprovordDao approvordDao;
    @Autowired
    private CalendarsDao calendarsDao;
    @Autowired
    private ApptemordService apptemordService;
    @Autowired
    private ApptemordaDao apptemordaDao;
    @Autowired
    private ApprovordaDao approvordaDao;
    @Autowired
    private MmordDao mmordDao;
    @Autowired
    private MachineDao machineDao;
    @Autowired
    private NmmordaDao nmmordaDao;
    @Autowired
    private NmmordDao nmmordDao;
    @Autowired
    private AccountDao accountDao;
    @Autowired
    private WageDao wageDao;

    /**
     * 分页查询
     */
    public PageResult<ApprovordVO> queryPage(ApprovordQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<ApprovordVO> list = approvordDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    /**
     * 添加
     */
    @Transactional
    public ResponseDTO<Long> add(ApprovordAddForm addForm) {

        ApprovordEntity approvordEntity = SmartBeanUtil.copy(addForm, ApprovordEntity.class);
        ApptemordEntity info = apptemordService.infoByTabId(addForm.getTabName());
        approvordEntity.setApprovLineNum(2);
        approvordDao.insert(approvordEntity);
        RequestUser requestUser = SmartRequestUtil.getRequestUser();
        ApprovordaEntity approvordaEntity = new ApprovordaEntity();
        approvordaEntity.setDocEntry(approvordEntity.getDocEntry());
        approvordaEntity.setLineNum(1);
        approvordaEntity.setUserId(requestUser.getUserId());
        approvordaEntity.setUserName(requestUser.getUserName());
        approvordaEntity.setLineStatus("C");
        approvordaEntity.setBeginDate(LocalDateTime.now());
        approvordaEntity.setPhaseName("发起");
        approvordaDao.insert(approvordaEntity);
        int lineNum = 1;
        for (ApptemordaEntity apptemordaEntity : info.getLista()) {
            lineNum++;
            ApprovordaEntity approvordaEntitya = new ApprovordaEntity();
            approvordaEntitya.setDocEntry(approvordEntity.getDocEntry());
            approvordaEntitya.setLineNum(lineNum);
            approvordaEntitya.setUserId(apptemordaEntity.getUserId());
            approvordaEntitya.setUserName(apptemordaEntity.getUserName());
            approvordaEntitya.setPhaseName(apptemordaEntity.getPhaseName());
            approvordaEntitya.setLineStatus("O");
            if (lineNum == 2) {
                approvordaEntitya.setLineStatus("L");
                approvordaEntitya.setBeginDate(LocalDateTime.now());
            }
            approvordaDao.insert(approvordaEntitya);
        }
        ApprovordaEntity approvordaEntityb = new ApprovordaEntity();
        approvordaEntityb.setDocEntry(approvordEntity.getDocEntry());
        approvordaEntityb.setLineNum(lineNum + 1);
        approvordaEntityb.setLineStatus("O");
        approvordaEntityb.setPhaseName("结束");
        approvordaDao.insert(approvordaEntityb);


        // 日历

        if (addForm.getTabName().equals("machine")) {
            CalendarsEntity calendarsEntity = new CalendarsEntity();
            MachineEntity machineEntity = new MachineEntity();
            machineEntity.setDocEntry(addForm.getBaseEntry());
            machineEntity.setDocStatus("L");
            machineEntity.setVersion(addForm.getBaseVersion());
            if (machineDao.updateById(machineEntity) == 0) {
                throw new RuntimeException("数据异常，刷新重试！");
            }
        } else if (addForm.getTabName().equals("calendars")) {
            CalendarsEntity calendarsEntity = new CalendarsEntity();
            calendarsEntity.setDocEntry(addForm.getBaseEntry());
            calendarsEntity.setDocStatus("L");
            calendarsEntity.setVersion(addForm.getBaseVersion());
            if (calendarsDao.updateById(calendarsEntity) == 0) {
                throw new RuntimeException("数据异常，刷新重试！");
            }
        } else if (addForm.getTabName().equals("mmord")) {
            MmordEntity mmordEntity = new MmordEntity();
            mmordEntity.setDocEntry(addForm.getBaseEntry());
            mmordEntity.setDocStatus("L");
            mmordEntity.setVersion(addForm.getBaseVersion());
            if (mmordDao.updateById(mmordEntity) == 0) {
                throw new RuntimeException("数据异常，刷新重试！");
            }
        } else if (approvordEntity.getTabName().equals("nmmord")) {
            NmmordEntity nmmordEntity = new NmmordEntity();
            nmmordEntity.setDocEntry(addForm.getBaseEntry());
            nmmordEntity.setDocStatus("L");
            nmmordEntity.setVersion(addForm.getBaseVersion());
            if (nmmordDao.updateById(nmmordEntity) == 0) {
                throw new RuntimeException("数据异常，刷新重试！");
            }

        } else if (approvordEntity.getTabName().equals("account")) {

            AccountEntity accountEntity = new AccountEntity();
            accountEntity.setDocEntry(addForm.getBaseEntry());
            accountEntity.setDocStatus("L");
            accountEntity.setVersion(addForm.getBaseVersion());
            if (accountDao.updateById(accountEntity) == 0) {
                throw new RuntimeException("数据异常，刷新重试！");
            }

        } else if (approvordEntity.getTabName().equals("wage")) {
            NmmordEntity nmmordEntity = new NmmordEntity();
            WageEntity wageEntity = new WageEntity();
            wageEntity.setDocEntry(addForm.getBaseEntry());
            wageEntity.setDocStatus("L");
            wageEntity.setVersion(addForm.getBaseVersion());
            if (wageDao.updateById(wageEntity) == 0) {
                throw new RuntimeException("数据异常，刷新重试！");
            }
        }


        return ResponseDTO.ok(approvordEntity.getDocEntry());

    }

    /**
     * 更新
     */

    @Transactional
    public ResponseDTO<String> update(ApprovordUpdateForm updateForm) {
        ApprovordEntity approvordEntity = SmartBeanUtil.copy(updateForm, ApprovordEntity.class);
        /*拒绝审核*/
        if ("X".equals(approvordEntity.getDocStatus())) {
            ApprovordaEntity approvordaEntity1 = approvordEntity.getLista().get(approvordEntity.getApprovLineNum() - 1);
            approvordaEntity1.setBeginDate(LocalDateTime.now());
            approvordaEntity1.setLineStatus("X");
            approvordaDao.update(approvordaEntity1, new LambdaQueryWrapper<ApprovordaEntity>().eq(true, ApprovordaEntity::getDocEntry, approvordaEntity1.getDocEntry()).eq(true, ApprovordaEntity::getLineNum, approvordaEntity1.getLineNum()));
            if (approvordDao.updateById(approvordEntity) == 0) {
                throw new RuntimeException("数据已被更新，刷新后重试！");
            }
            return ResponseDTO.ok();
        } else {
            ApprovordaEntity approvordaEntity = approvordEntity.getLista().get(approvordEntity.getApprovLineNum() - 1);
            approvordaEntity.setLineStatus("C");
            approvordaDao.update(approvordaEntity, new LambdaQueryWrapper<ApprovordaEntity>().eq(true, ApprovordaEntity::getDocEntry, approvordaEntity.getDocEntry()).eq(true, ApprovordaEntity::getLineNum, approvordaEntity.getLineNum()));
            ApprovordaEntity approvordaEntity1 = approvordEntity.getLista().get(approvordEntity.getApprovLineNum());
            approvordaEntity1.setBeginDate(LocalDateTime.now());

            /*最后一次审核 */
            if (updateForm.getApprovLineNum() == updateForm.getLista().size() - 1) {
                /* 审核*/
                if (updateForm.getTabName().equals("machine")) {
                    if ("A".equals(updateForm.getApprovType())) {
                        MachineEntity machineEntity = new MachineEntity();
                        machineEntity.setDocEntry(updateForm.getBaseEntry());
                        machineEntity.setDocStatus("O");
                        machineEntity.setApproved(true);
                        machineDao.updateById(machineEntity);
                    } else if ("B".equals(updateForm.getApprovType())) {
                        MachineEntity machineEntity = new MachineEntity();
                        machineEntity.setDocEntry(updateForm.getBaseEntry());
                        machineEntity.setDocStatus("O");
                        machineEntity.setApproved(false);
                        machineDao.updateById(machineEntity);
                    }
                } else if (updateForm.getTabName().equals("calendars")) {
                    if ("A".equals(updateForm.getApprovType())) {
                        CalendarsEntity calendarsEntity = new CalendarsEntity();
                        calendarsEntity.setDocEntry(updateForm.getBaseEntry());
                        calendarsEntity.setDocStatus("O");
                        calendarsEntity.setApproved(true);
                        calendarsDao.updateById(calendarsEntity);

                    } else if ("B".equals(updateForm.getApprovType())) {
                        CalendarsEntity calendarsEntity = new CalendarsEntity();
                        calendarsEntity.setDocEntry(updateForm.getBaseEntry());
                        calendarsEntity.setDocStatus("O");
                        calendarsEntity.setApproved(false);
                        calendarsDao.updateById(calendarsEntity);
                    }
                } else if (updateForm.getTabName().equals("mmord")) {
                    if ("A".equals(updateForm.getApprovType())) {

                        MmordEntity mmordEntity = new MmordEntity();
                        mmordEntity.setDocEntry(updateForm.getBaseEntry());
                        mmordEntity.setDocStatus("O");
                        mmordEntity.setApproved(true);
                        mmordDao.updateById(mmordEntity);

                    } else if ("B".equals(updateForm.getApprovType())) {
                        MmordEntity mmordEntity = new MmordEntity();
                        mmordEntity.setDocEntry(updateForm.getBaseEntry());
                        mmordEntity.setDocStatus("O");
                        mmordEntity.setApproved(false);
                        mmordDao.updateById(mmordEntity);
                    }
                } else if (approvordEntity.getTabName().equals("nmmord")) {
                    if ("A".equals(updateForm.getApprovType())) {
                        NmmordEntity nmmordEntity = new NmmordEntity();
                        nmmordEntity.setDocEntry(updateForm.getBaseEntry());
                        nmmordEntity.setDocStatus("O");
                        nmmordEntity.setApproved(true);
                        nmmordDao.updateById(nmmordEntity);
                    } else if ("B".equals(updateForm.getApprovType())) {
                        NmmordEntity nmmordEntity = new NmmordEntity();
                        nmmordEntity.setDocEntry(updateForm.getBaseEntry());
                        nmmordEntity.setDocStatus("O");
                        nmmordEntity.setApproved(false);
                        nmmordDao.updateById(nmmordEntity);
                    }

                } else if (approvordEntity.getTabName().equals("account")) {
                    if ("A".equals(updateForm.getApprovType())) {
                        AccountEntity accountEntity = new AccountEntity();
                        accountEntity.setDocEntry(updateForm.getBaseEntry());
                        accountEntity.setDocStatus("O");
                        accountEntity.setApproved(true);
                        accountDao.updateById(accountEntity);
                    } else if ("B".equals(updateForm.getApprovType())) {
                        AccountEntity accountEntity = new AccountEntity();
                        accountEntity.setDocEntry(updateForm.getBaseEntry());
                        accountEntity.setDocStatus("O");
                        accountEntity.setApproved(false);
                        accountDao.updateById(accountEntity);
                    }
                } else if (approvordEntity.getTabName().equals("wage")) {
                    if ("A".equals(updateForm.getApprovType())) {
                        WageEntity wageEntity = new WageEntity();
                        wageEntity.setDocEntry(updateForm.getBaseEntry());
                        wageEntity.setDocStatus("O");
                        wageEntity.setApproved(true);
                        wageDao.updateById(wageEntity);
                    } else if ("B".equals(updateForm.getApprovType())) {
                        WageEntity wageEntity = new WageEntity();
                        wageEntity.setDocEntry(updateForm.getBaseEntry());
                        wageEntity.setDocStatus("O");
                        wageEntity.setApproved(false);
                        wageDao.updateById(wageEntity);
                    }
                }
                approvordaEntity1.setLineStatus("C");
                approvordEntity.setDocStatus("C");

            } else {
                approvordaEntity1.setLineStatus("L");
            }
            approvordaDao.update(approvordaEntity1, new LambdaQueryWrapper<ApprovordaEntity>().eq(true, ApprovordaEntity::getDocEntry, approvordaEntity1.getDocEntry()).eq(true, ApprovordaEntity::getLineNum, approvordaEntity1.getLineNum()));
            approvordEntity.setApprovLineNum(approvordEntity.getApprovLineNum() + 1);
            if (approvordDao.updateById(approvordEntity) == 0) {
                throw new RuntimeException("数据已被更新，刷新后重试！");
            }
            return ResponseDTO.ok();
        }
    }

    /**
     * 批量删除
     */
    public ResponseDTO<String> batchDelete(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return ResponseDTO.ok();
        }

        approvordDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    @Transactional
    public ResponseDTO<String> delete(Long docEntry) {
        ApprovordEntity approvordEntity = approvordDao.selectById(docEntry);

        if (approvordEntity.getTabName().equals("machine")) {
            MachineEntity machineEntity = machineDao.selectById(approvordEntity.getBaseEntry());
            machineEntity.setDocStatus("O");
            if (machineDao.updateById(machineEntity) == 0) {
                throw new RuntimeException("数据异常，刷新重试！");
            }
        } else if (approvordEntity.getTabName().equals("calendars")) {
            CalendarsEntity calendarsEntity = calendarsDao.selectById(approvordEntity.getBaseEntry());
            calendarsEntity.setDocStatus("O");
            if (calendarsDao.updateById(calendarsEntity) == 0) {
                throw new RuntimeException("数据异常，刷新重试！");
            }
        } else if (approvordEntity.getTabName().equals("mmord")) {
            MmordEntity mmordEntity = mmordDao.selectById(approvordEntity.getBaseEntry());
            mmordEntity.setDocStatus("O");
            if (mmordDao.updateById(mmordEntity) == 0) {
                throw new RuntimeException("数据异常，刷新重试！");
            }
        } else if (approvordEntity.getTabName().equals("nmmord")) {
            NmmordEntity nmmordEntity = nmmordDao.selectById(approvordEntity.getBaseEntry());
            nmmordEntity.setDocStatus("O");
            if (nmmordDao.updateById(nmmordEntity) == 0) {
                throw new RuntimeException("数据异常，刷新重试！");
            }
        }else if (approvordEntity.getTabName().equals("account")) {
            AccountEntity accountEntity = accountDao.selectById(approvordEntity.getBaseEntry());
            accountEntity.setDocStatus("O");
            if (accountDao.updateById(accountEntity) == 0) {
                throw new RuntimeException("数据异常，刷新重试！");
            }
        }else if (approvordEntity.getTabName().equals("wage")) {
            WageEntity wageEntity = wageDao.selectById(approvordEntity.getBaseEntry());
            wageEntity.setDocStatus("O");
            if (wageDao.updateById(wageEntity) == 0) {
                throw new RuntimeException("数据异常，刷新重试！");
            }
        }
        approvordDao.deleteById(docEntry);
        approvordaDao.deleteById(docEntry);




        return ResponseDTO.ok();
    }

    public List<ApprovordEntity> info(ApprovordQueryForm queryForm) {
        return approvordDao.info(queryForm.getTabName(), queryForm.getBaseEntry());
    }
}
